package com.yixun.controller;

import com.yixun.entity.YxUserRole;
import com.yixun.service.UserRoleService;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

@Controller
@RequestMapping(value = "/userRole")
public class UserRoleController extends BaseController {

    private final UserRoleService userRoleService;

    public UserRoleController(UserRoleService userRoleService) {
        this.userRoleService = userRoleService;
    }

    /**
     * 给用户添加角色
     */
    @RequestMapping(value = "/insertUserRole", method = RequestMethod.POST)
    @ResponseBody
    public boolean insertUserRole(@RequestBody YxUserRole userRole){
        int row = userRoleService.insertUserRole(this.getSessionUser(), userRole);
        return row > 0;
    }

    /**
     * 根据用户id获取用户角色id
     */
    @RequestMapping(value = "/getUserRoleID/{user_id}")
    @ResponseBody
    public String getUserRoleID(@PathVariable(value = "user_id") String user_id){
        return "{\"result\":\""+userRoleService.getUserRoleID(user_id)+"\"}";
    }

    @RequestMapping(value = "/go_select_user")
    public String go_select_user(String role_id, String handle_name, Model model){
        model.addAttribute("role_id", role_id);
        model.addAttribute("handle_name", handle_name);
        model.addAttribute("dept_id", this.getSessionUser().getDept_id());
        return "org/user/select_user";
    }

    /**
     * 根据角色id获取对应的用户id
     */
    @RequestMapping(value = "/getUserIdsByRoleId/{r_id}")
    @ResponseBody
    public String getUserIdsByRoleId(@PathVariable(value = "r_id") String r_id){
        return "{\"result\":\""+ userRoleService.getUserIdsByRoleId(r_id) +"\"}";
    }

    /**
     * 给角色关联用户
     */
    @RequestMapping(value = "/insertUserRoleByRole", method = RequestMethod.POST)
    @ResponseBody
    public boolean insertUserRoleByRole(@RequestBody Map map){
        int row = userRoleService.insertUserRoleByRole(this.getSessionUser(), map);
        return row > 0;
    }
}
